package com.fypg.module.medical.service.hospitalarea;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.fypg.module.medical.controller.admin.hospitalarea.vo.*;
import com.fypg.module.medical.dal.dataobject.hospitalarea.HospitalAreaDO;
import com.fypg.framework.common.pojo.PageResult;
import com.fypg.framework.common.pojo.PageParam;
import com.fypg.framework.common.util.object.BeanUtils;

import com.fypg.module.medical.dal.mysql.hospitalarea.HospitalAreaMapper;

import static com.fypg.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.fypg.framework.common.util.collection.CollectionUtils.convertList;
import static com.fypg.framework.common.util.collection.CollectionUtils.diffList;
import static com.fypg.module.medical.enums.ErrorCodeConstants.*;

/**
 * 医院区域信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class HospitalAreaServiceImpl implements HospitalAreaService {

    @Resource
    private HospitalAreaMapper hospitalAreaMapper;

    @Override
    public String createHospitalArea(HospitalAreaSaveReqVO createReqVO) {
        // 插入
        HospitalAreaDO hospitalArea = BeanUtils.toBean(createReqVO, HospitalAreaDO.class);
        hospitalAreaMapper.insert(hospitalArea);
        // 返回
        return hospitalArea.getId();
    }

    @Override
    public void updateHospitalArea(HospitalAreaSaveReqVO updateReqVO) {
        // 校验存在
        validateHospitalAreaExists(updateReqVO.getId());
        // 更新
        HospitalAreaDO updateObj = BeanUtils.toBean(updateReqVO, HospitalAreaDO.class);
        hospitalAreaMapper.updateById(updateObj);
    }

    @Override
    public void deleteHospitalArea(String id) {
        // 校验存在
        validateHospitalAreaExists(id);
        // 删除
        hospitalAreaMapper.deleteById(id);
    }

    @Override
        public void deleteHospitalAreaListByIds(List<String> ids) {
        // 校验存在
        validateHospitalAreaExists(ids);
        // 删除
        hospitalAreaMapper.deleteByIds(ids);
        }

    private void validateHospitalAreaExists(List<String> ids) {
        List<HospitalAreaDO> list = hospitalAreaMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(HOSPITAL_AREA_NOT_EXISTS);
        }
    }

    private void validateHospitalAreaExists(String id) {
        if (hospitalAreaMapper.selectById(id) == null) {
            throw exception(HOSPITAL_AREA_NOT_EXISTS);
        }
    }

    @Override
    public HospitalAreaDO getHospitalArea(String id) {
        return hospitalAreaMapper.selectById(id);
    }

    @Override
    public PageResult<HospitalAreaDO> getHospitalAreaPage(HospitalAreaPageReqVO pageReqVO) {
        return hospitalAreaMapper.selectPage(pageReqVO);
    }

}